% 9.21 Lecture 7 Question 1
clear,clc,close all

% ------------------- step 1 ------------------- %
w1 = unifrnd(-0.5, 0.5, 1, 1);
w2 = unifrnd(-0.5, 0.5, 1, 1);

% ------------------- step 2, 3, 4 ------------------- %
% Threshold: theta
theta = 0;
% learning rate: a
a = 0.1;
% desired output: Yd
Yd = [0 0 1 1];
% Inputs dots: x
dot1 = [-0.5 0.5];
dot2 = [-0.2 -0.1];
dot3 = [0.2 0.25];
dot4 = [0.8 0.8];
% Draw inputs
grid on, hold on
plot(dot1(1), dot1(2), 'o-k', 'MarkerFaceColor','k')
plot(dot2(1), dot2(2), 'o-k', 'MarkerFaceColor','k')
plot(dot3(1), dot3(2), 'o-k', dot4(1), dot4(2), 'o-k')
% Training
p_max = 10;  % epoch
Y = zeros(p_max, 4);
for p = 1:p_max
    Y(p, 1) = step_fun(dot1(1) * w1 + dot1(2) * w2 - theta);
    w1 = w1 + a * dot1(1) * (Yd(1) - Y(p, 1));
    w2 = w2 + a * dot1(2) * (Yd(1) - Y(p, 1));
    Y(p, 2) = step_fun(dot2(1) * w1 + dot2(2) * w2 - theta);
    w1 = w1 + a * dot2(1) * (Yd(2) - Y(p, 2));
    w2 = w2 + a * dot2(2) * (Yd(2) - Y(p, 2));
    Y(p, 3) = step_fun(dot3(1) * w1 + dot3(2) * w2 - theta);
    w1 = w1 + a * dot3(1) * (Yd(3) - Y(p, 3));
    w2 = w2 + a * dot3(2) * (Yd(3) - Y(p, 3));
    Y(p, 4) = step_fun(dot4(1) * w1 + dot4(2) * w2 - theta);
    w1 = w1 + a * dot4(1) * (Yd(4) - Y(p, 4));
    w2 = w2 + a * dot4(2) * (Yd(4) - Y(p, 4));
    disp(Y(p,:))
end
% Draw training results
x1 = -0.5:0.1:1;
x2 = -(w1/w2)*x1 + theta/w2;
plot(x1, x2, '-r')
xlim([-0.5 1])
ylim([-0.1 0.8])

function [ output ] = step_fun( input )
% activation function: step
    if input >= 0
        output = 1;
    else
        output = 0;
    end
end
